package com.javasm.controller;

import com.javasm.domain.ResponseResult;
import com.javasm.domain.User;
import com.javasm.service.LoginService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author：MoDebing
 * @Version：1.0
 * @Date：2022-09-01-18:03
 * @Description: 用户登录相关的controller
 */
@RestController
@RequestMapping("user")
public class LoginController {

    private final LoginService loginService;

    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }

    @PostMapping("/login")
    public ResponseResult<User> login(@RequestBody User user){
        //登录
        return loginService.login(user);

    }

    @RequestMapping("/logout")
    public ResponseResult logout(){
        return loginService.logout();
    }

    @RequestMapping("/401")
    public ResponseResult loginError(HttpServletRequest request){
        // 取出错误信息
        String errorMessage = (String) request.getAttribute("errorMessage");
        return new ResponseResult(401,errorMessage);
    }
}
